#include <string.h>
#include <iostream>
using namespace std;
class String
{
public:
	String()
    {   
        _pstr=new char[1]();
        _pstr[0]='0';
    }
    //构造函数
	String(const char *pstr)
    :_pstr(new char[strlen(pstr)+1])
    {
        strcpy(_pstr,pstr);
    }
    //拷贝构造函数
	String(const String &rhs)
    :_pstr(new char[strlen(rhs._pstr)+1])
    {
        strcpy(_pstr,rhs._pstr);
    }
    //赋值运算符函数
    String &operator=(const String &rhs){
        if(this!=&rhs){
            //赋值
            _pstr=new char[strlen(rhs._pstr)+1]();
            strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }
    //给当前对象的末尾拼接另一个对象的内容
    String & append(const String & rhs){
        cout<<"rhs"<<endl;
       _pstr=new char[strlen(_pstr)+strlen(rhs._pstr)+1];
       strcat(_pstr,rhs._pstr);
       return *this;
    }
    //实现C风格字符串直接拼接在当前对象
    String & append(const char * pstr){
    cout<<"pstr"<<endl;
    _pstr=new char[strlen(_pstr)+strlen(pstr)+1];
    strcat(_pstr,pstr);
    return *this;
    }
    ~String(){
        delete [] _pstr;
        _pstr=nullptr;
        cout<<"delete"<<endl;
    }
    void print(){
        cout<<_pstr<<endl;
    }
    size_t length() const{
        cout<<"length"<<endl;
        int a=strlen(_pstr);
        return a;
    }
    const char * c_str() const{
        cout<<"c_str"<<endl;
        return _pstr;
    }

private:
	char * _pstr;
};

int main()
{
	String str1;
	str1.print();
	

	String str2 = "Hello,world";
	String str3("wangdao");
	
    str2.print();		
	str3.print();	
	
	String str4 = str3;
	str4.print();
	
	str4 = str2;
	str4.print();
	cout<<"==========="<<endl;
    str1.append("hello");
    str1.print();
	cout<<"==========="<<endl;
    str1.append(str2);
    str1.print();
    cout<<"==========="<<endl;
    int a=str1.length();
    cout<<a<<endl;
    cout<<"==========="<<endl;
    const char * p;
    p=str1.c_str();
    cout<<p<<endl;
    p=nullptr;
	return 0;
}
